programming4us
           
 
 
Windows

Windows Azure : Programming Access Control Service (part 9) - Configuring a Web Service Client to Acquire and Send SAML Tokens

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/4/2010 11:54:01 AM
2.2. Configuring a Web Service Client to Acquire and Send SAML Tokens

In this section, you see the client application that acquires a SAML token from LocalSTS and sends it to ACS. Listing 13 shows the code in the main function of the Program.cs file from the Client project.

Example 13. Program.cs in Client Project
private const string ServiceNamespace = "proazure-1";
private const string AcsHostName = "accesscontrol.windows.net";
private const string StsBaseAddress = "localhost/localsts";
private const string StsPath = "Trust/13/UserName";
public static void Main(string[] args)
{
string stsAddress = string.Format("https://{0}/{1}",
StsBaseAddress, StsPath);
string acsSTSAddress = string.Format
("https://{0}.{1}/WRAPv0.8", ServiceNamespace, AcsHostName);
string samlAssertion = GetSamlAssertion(stsAddress, acsSTSAddress);
string acsToken = GetACSToken(samlAssertion);

// create the binding and address to communicate with the service
WebHttpBinding binding =
new WebHttpBinding(WebHttpSecurityMode.None);
Uri address = new Uri(@"http://localhost/acsexample");

WebChannelFactory<IACSExample> channelFactory =
new WebChannelFactory<IACSExample>(binding, address);

IACSExample proxy = channelFactory.CreateChannel();

using (new OperationContextScope(proxy as IContextChannel))
{
string authHeaderValue = "WRAPv0.8" + " "
+ HttpUtility.UrlDecode(acsToken);

WebOperationContext.Current.OutgoingRequest.Headers
.Add("authorization", authHeaderValue);



// call the service and get a response
try
{
Console.Write("\tCalling GetMachineName: ");
Console.WriteLine("Machine Name is:"
+ proxy.GetMachineName());

Console.Write("\tCalling GetUserDomainName: ");
Console.WriteLine("User Domain Name is:"
+ proxy.GetUserDomainName());

Console.Write("\tCalling GetOSVersion: ");
Console.WriteLine("OS Version is:" + proxy.GetOSVersion());
Console.Write("\tCalling EncodeString: ");
Console.WriteLine("Encoded String is:"
+ Encoding.UTF8.GetString(proxy.EncodeString("Welcome to ProAzure.")));
}
catch (MessageSecurityException ex)
{
if (ex.InnerException != null)
{
WebException wex = ex.InnerException as WebException;
if (wex != null)
{
Console.WriteLine("Error: {0}", wex.Message);
}
}
else
{
throw;
}
}
}

((IClientChannel)proxy).Close();

channelFactory.Close();

Console.ReadLine();
}


The GetSamlAssertion() function retrieves the SAML token from LocalSTS, and GetACSToken() sends the SAML token to ACS and returns an ACS token. Listing 14 shows the code for the GetSamlAssertion() and GetACSToken() functions.

Example 14. GetSamlAssertion and GetACSToken
private static string GetSamlAssertion(string stsAddress, string acsStsAddress)
{
WSTrustChannelFactory trustChannelFactory = new WSTrustChannelFactory(
new WindowsWSTrustBinding
(SecurityMode.TransportWithMessageCredential),
new EndpointAddress(new Uri(stsAddress)));
trustChannelFactory.TrustVersion = TrustVersion.WSTrust13;

RequestSecurityToken rst =
new RequestSecurityToken(WSTrust13Constants.RequestTypes.Issue,
WSTrust13Constants.KeyTypes.Bearer);
rst.AppliesTo = new EndpointAddress(acsStsAddress);
rst.TokenType = Microsoft.IdentityModel.Tokens.
SecurityTokenTypes.Saml2TokenProfile11;

WSTrustChannel channel =
(WSTrustChannel)trustChannelFactory.CreateChannel();
GenericXmlSecurityToken token =
channel.Issue(rst) as GenericXmlSecurityToken;

return token.TokenXml.OuterXml;
}

private static string GetACSToken(string samlAssertion)
{
WebClient tokenClient = new WebClient();
tokenClient.BaseAddress =
string.Format("https://{0}.{1}", ServiceNamespace, AcsHostName);

NameValueCollection values = new NameValueCollection();
values.Add("wrap_SAML", samlAssertion);
values.Add("applies_to", "http://localhost/acsexample");

byte[] responseBytes = tokenClient.UploadValues("WRAPv0.8", values);
string response = Encoding.UTF8.GetString(responseBytes);

return response
.Split('&')
.Single(value => value.StartsWith
("wrap_token=", StringComparison.OrdinalIgnoreCase))
.Split('=')[1];
}


To run the example, do the following:

  1. Run the Service project.

  2. Run the client from the ACSwithSAML solution folder.

Other -----------------
- Windows 7 : Working with Registry Entries (part 3)
- Windows 7 : Working with Registry Entries (part 2)
- Windows 7 : Working with Registry Entries (part 1) - Changing the Value of a Registry Entry
- Windows 7 : Keeping the Registry Safe
- Windows 7 : Getting to Know the Registry (part 2)
- Windows 7 : Getting to Know the Registry (part 1) - Understanding Registry Settings
- Windows 7 : Firing Up the Registry Editor
- Windows Azure : Managing Access Control Service Resources (part 2)
- Windows Azure : Managing Access Control Service Resources (part 1)
- Windows Azure : Access Control Service Management Portal
- Windows 7 : Reset a Broken Service
- Windows 7 : Make Windows Shut Down Services Faster
- Windows 7 : Disable Services for Faster Performance
- Windows 7 : Controlling Services with a Script
- Windows 7 : Controlling Services at the Command Prompt
- Windows 7 : Controlling Services with the Services Snap-In
- Windows Azure : Access Control Service Usage Scenarios (part 3)
- Windows Azure : Access Control Service Usage Scenarios (part 2)
- Windows Azure : Access Control Service Usage Scenarios (part 1)
- Windows Azure : Access Control Service - Claims-Based Identity Model
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us